package com.nbcio.modules.erp.voucher.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbcio.common.core.domain.R;
import com.nbcio.common.mybatis.core.page.PageQuery;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.modules.erp.voucher.domain.ErpAccount;
import com.nbcio.modules.erp.voucher.service.IErpAccountService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会计科目控制器
 */
@RestController
@RequestMapping("/erp/account")
public class ErpAccountController {

    @Resource
    private IErpAccountService accountService;

    /**
     * 分页查询科目列表
     */
    @GetMapping("/page")
    public R<TableDataInfo<ErpAccount>> page(ErpAccount account, PageQuery pageQuery) {
        return R.ok(accountService.queryPageList(account, pageQuery));
    }

    /**
     * 查询科目树列表
     */
    @GetMapping("/tree")
    public R<List<ErpAccount>> tree() {
        return R.ok(accountService.queryTreeList());
    }

    /**
     * 根据ID获取科目
     */
    @GetMapping("/{id}")
    public R<ErpAccount> get(@PathVariable("id") String id) {
        return R.ok(accountService.getById(Long.parseLong(id)));
    }

    /**
     * 根据编码获取科目
     */
    @GetMapping("/code/{code}")
    public R<ErpAccount> getByCode(@PathVariable("code") String code) {
        return R.ok(accountService.getByCode(code));
    }

    /**
     * 新增科目
     */
    @PostMapping
    public R<Boolean> create(@RequestBody ErpAccount account) {
        return R.ok(accountService.save(account));
    }

    /**
     * 更新科目
     */
    @PutMapping("/{id}")
    public R<Boolean> update(@PathVariable("id") String id, @RequestBody ErpAccount account) {
        account.setAccountId(Long.parseLong(id));
        return R.ok(accountService.updateById(account));
    }

    /**
     * 删除科目
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable("id") String id) {
        return R.ok(accountService.removeById(Long.parseLong(id)));
    }
}

